define(function(require, exports, module) {
  'use strict';

  var Alert = require('alert');
  var Tips = require('tips');

  var DialogForm = require('../../common/form/dialogform');
  var io = require('../../common/io');

  var DataSourceForm = module.exports = DialogForm.extend({
    defaults: {
      isEdit: false,
      delegates: {

      },
      formOptions: {
        url: 'coverSave',
        events: {
          'after:render': function() {
            this.validate.option('isEdit', this.option('isEdit'));

          }
        }
      },
      events: {
        done: function(e, data) {
          new Tips({
            content: this.option('isEdit') ? '修改成功' : '添加成功'
          });
          this.fire('success', data);
        },
        fail: function(e, msg) {
          new Alert({
            title: '提示',
            content: msg
          });
        }
      }
    },
    setup: function() {
      var isEdit = this.option('isEdit') || false;

      this.option('formOptions/data/groups', generateGroups(this.option('coverData')));
      this.option('formOptions/isEdit', isEdit);
      if (isEdit) {
        this.option('formData/docId', this.option('docId'));
      }
      DataSourceForm.superclass.setup.apply(this);
    }

  });

  function generateGroups(data) {
    return [{
      label: '封面名称',
      attrs: {
        type: 'text',
        name: 'docSubject',
        value: data ? data.docSubject : '',
        maxlength: 50,
        //pattern: '^[\u4e00-\u9fa5]+$',
        required: 'required',
        'data-role': 'docSubject'
      }
    }, {
      label: '上传图片',
      attrs: {
        type: 'file',
        name: 'src',
        accept: 'application/image',
        pattern: '^+\\.(png|jpg|gif)$',
        required: 'required',
        value: data ? data.src : ''
      }
    }];
  }
});
